<?php
/**
 * @name myMsSQL Project
 * @copyright GPL
 * @author Hakan DAMAR
 * @abstract System Class
 */
require_once 'interface/ISYSTEM.php'; //SYSTEM INTERFACE
require_once 'inc/dbConnection.php'; //DB CLASS
require_once 'EXCEPTION.php'; // EXCEPTION_HANDLER CLASS

final class SYSTEM  implements ISYSTEM{
	
	private static $DB;
	private static $sysObject;

	private $addTableType;
	private $tableOwner;
	
	/**
	 * SYSTEM Class Construction
	 * @return void
	 */
	private function __construct(){
		ob_start();
		$this->debugMode();
		$this->__isset(!SYSTEM::$DB) ? SYSTEM::$DB = DB::_getDBObject() : null;
	}
	
	/**
	 * Singleton Pattern
	 * @return System Object
	 */
	public static function _getSysObject(){
		if(!isset(self::$sysObject)){
			$class = __CLASS__;
			self::$sysObject = new $class;
		}
		return self::$sysObject;
	}
	
 	/**
 	 * SYSTEM Debug Mode Controller
 	 * @param bool $debugMode
 	 * @return void
 	 */
 	public function debugMode($debugMode=false){
 		if(!$debugMode){
 			// Loging On
 			sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_ALL);
 			// Log Setting
 			sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR | SQLSRV_LOG_SEVERITY_WARNING);
 			
 			if(!isset($_SESSION)){
 				class_exists("EXCEPTION_HANDLER") != true ? exit("Exception Module Not Loaded!") : null;
 				EXCEPTION_HANDLER::_setMsg("006");
 				EXCEPTION_HANDLER::_setMsg("007");
 				exit;
 			}
 		}
 	}
 	
 	/**
 	 * (non-PHPdoc)
 	 * @see lib/ISYSTEM#getDBList($allDB, $addSize)
 	 */
 	public function getDBList($allDB, $addSize){
		$query = SYSTEM::$DB->execQuery("sp_databases");
		while($row=sqlsrv_fetch_array($query)){
			$dbName = $row["DATABASE_NAME"];
			$dbSize = $row["DATABASE_SIZE"];
			print $dbName." ($dbSize)<br>";
		}
		
 	}
 	
 	/**
 	 * (non-PHPdoc)
 	 * @see lib/ISYSTEM#getTableList($addTableType, $tableOwner)
 	 */
 	public function getTableList($addTableType,$tableOwner="dbo"){
 		$addTableType != "" ? $this->addTableType = strtoupper($addTableType) : exit("Error: Pls, enter table type!");
 		!is_int($tableOwner) ? $this->tableOwner = $tableOwner : exit("Error: Pls, enter table owner!");
 		
 		$query = SYSTEM::$DB->execQuery("sp_tables");
		while($row=sqlsrv_fetch_array($query)){
			if($row["TABLE_OWNER"] != $this->tableOwner){ break; }
			if(($row["TABLE_TYPE"] != $this->addTableType) && ($this->addTableType != "ALL")){ break; }						
				$tblQualifier = $row["TABLE_QUALIFIER"];
				$tblOwner     = $row["TABLE_OWNER"];
				$tblName      = $row["TABLE_NAME"];
				$tblType      = $row["TABLE_TYPE"];
								
			print $tblOwner.".".$tblQualifier." - ".$tblName." - ".$tblType."<br>";
		}
 	}
 	
 	/**
 	 * (non-PHPdoc)
 	 * @see lib/ISYSTEM#getColumsInfo($tableName, $tableOwner)
 	 */
 	public function getColumsInfo($tableName, $tableOwner){
		$query = $DB->execQuery("sp_columns @table_name = tbl_users, @table_owner = dbo");
		while($row=sqlsrv_fetch_array($query)){
			print_r($row);
		}
 	}
 	
 	/**
 	 * Type Controller
 	 * @param $variable
 	 * @return Type
 	 */
 	public static function typeController($variable){
 		is_string($variable) ? $variable  = "String" : null;
        is_int($variable)    ? $variable  = "Int"    : null;
        is_double($variable) ? $variable  = "Double" : null;
        is_bool($variable)   ? $variable  = "Bool"   : null;
        is_array($variable)  ? $variable  = "Array"  : null;
        is_float($variable)  ? $variable  = "Float"  : null;
        is_object($variable) ? $variable  = "Object" : null;
        is_numeric($variable)? $variable  = "Numeric": null;
        return $variable;
 	}
 	
 	/**
 	 * Custom Hash Alg.
 	 * @param $strData
 	 * @return password
 	 */
 	public static function customHash($strData){
 		self::typeController($strData) != "String" && $strData != "" ? exit("Type Error: This method only allow string data type!") : $strData = sha1($strData);
 		$randNumbers = sha1(rand(100000,999999));
 		$firstSalt   = substr($randNumbers,-6,6);
 		$secondSalt  = substr($randNumbers,3);
 		
 		$newPassword = "{".$firstSalt.$strData.$secondSalt."}";
		return $newPassword;
 	}
 	
 	/**
 	 * Class Information
 	 * @return string
 	 */
 	public function __toString(){
 		$classInfo = __CLASS__." | myMsSQL Main Class";
 		return $classInfo;
 	}
 	
 	/**
 	 * Input Data Controller
 	 * @param $data
 	 * @return String
 	 */
 	public static function inputController($data){
 		$controllerArr = array("'","\\","/","\"","&","%","$","(",")","<",">","~");
 		$controller = str_replace($controllerArr,"",$data);
 		$newData = trim($controller);
 		$safeData = htmlspecialchars(strip_tags($newData));
 		return $safeData; 		
 	}
 	
 	/**
 	 * Object exist controller
 	 * @param $objectName
 	 * @return void
 	 */
 	protected function __isset($objectName){
 		return isset($objectName);
 	}
 	
 	/**
 	 * Close the database server connection
 	 * @return void
 	 */
 	public function dbDisconnect(){
 		SYSTEM::$DB->disconnect();
 	}
 	
 	/**
 	 * Free result memory
 	 * @return void
 	 */
 	public function freeResultNow(){
 		SYSTEM::$DB->freeResult();
 	}
 	
 	/**
 	 * Flush buffer
 	 * @return void
 	 */
 	public function __destruct(){
 		ob_end_flush();
 	}

}

?>